//
//  LSLevinBookDetailViewController.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2019/8/21.
//  Copyright © 2019 wpkj. All rights reserved.
//

#import "LSLevinBookDetailViewController.h"
#import "LSLevinBookListModel.h"
#import "LSLevinBookDetailTableViewCell.h"

@interface LSLevinBookDetailViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *contentArray;
@property (nonatomic, strong) NSDictionary *fileClassifyDict;
@end

static NSString *LSLevinBookDetailTableViewCellID = @"LSLevinBookDetailTableViewCellID";

@implementation LSLevinBookDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"查看";
    [self.view addSubview:self.tableView];
}

- (void)setModel:(LSLevinBookListModel *)model {
    _model = model;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LSLevinBookDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LSLevinBookDetailTableViewCellID forIndexPath:indexPath];
    cell.mainTitleLabel.text = self.contentArray[indexPath.row];
    [cell.mainTitleLabel sizeToFit];
    cell.contentLabel.text = [self getContentForIndexPath:indexPath];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //获取文字内容宽度
    CGRect TitleRect = [self.contentArray[indexPath.section] boundingRectWithSize:CGSizeMake(ScreenWidth, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil];
    CGFloat width = ScreenWidth-12-12-TitleRect.size.width;
    CGRect TitleRectTrue = [[self getContentForIndexPath:indexPath] boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil];
    if (TitleRectTrue.size.height+20+4 > 44) {
        return TitleRectTrue.size.height + 20+4;
    } else {
        return 44;
    }
}

- (NSString *)getContentForIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
            return self.model.fileTitle;
            break;
        case 1:
            return self.model.fileNum;
            break;
        case 2:
            return self.model.fileCode;
            break;
        case 3:
            return self.model.fileCompany;
            break;
        case 4:
            return [ValidateUtils validateNull:[[ValidateUtils validateNull:self.fileClassifyDict[self.model.fileClassify]] isEqualToString:@""] ? self.model.fileClassify : self.fileClassifyDict[self.model.fileClassify]];
            break;
        case 5:
            return [self timeStampToDate:self.model.fileReceiveDate];
            break;
        case 6:
            return [ValidateUtils validateNull:self.model.isFiling.integerValue == 0 ? @"未归档" : @"已归档"];
            break;
        case 7:
            return self.model.fileRemark;
            break;
        default:
            return @"";
            break;
    }
}

- (NSString *)timeStampToDate:(NSString *)timeStampString {
    // iOS 生成的时间戳是10位
    if (![[ValidateUtils validateNull:timeStampString] isEqualToString:@""] && !([ValidateUtils validateNull:timeStampString].integerValue == 0)) {
        NSTimeInterval interval = [timeStampString doubleValue] / 1000.0;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        return [formatter stringFromDate:date];
    } else {
        return @"";
    }
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-SafeAreaTopHeight)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = hexColor(eeeeee);
        [_tableView registerClass:LSLevinBookDetailTableViewCell.class forCellReuseIdentifier:LSLevinBookDetailTableViewCellID];
    }
    return _tableView;
}

- (NSArray *)contentArray {
    if (!_contentArray) {
        _contentArray = @[@"文章标题：",
                          @"原文编号：",
                          @"字号：",
                          @"来文单位：",
                          @"收文分类：",
                          @"收文时间：",
                          @"是否归档：",
                          @"备注："];
    }
    return _contentArray;
}

- (NSDictionary *)fileClassifyDict {
    if (!_fileClassifyDict) {
        _fileClassifyDict = @{@"central":@"中央文件",
                              @"autonomousRegion":@"自治区文件",
                              @"MuniGovernment":@"市委市政府文件",
                              @"bureau":@"本局文件",
                              @"MuniDepartments":@"市直相关部门文件"
                              };
    }
    return _fileClassifyDict;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
